#for语句
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

for char in "hello":
    print(char)

person = {"name": "Alice", "age": 30, "city": "New York"}

# 遍历键
for key in person:
    print(key)

# 遍历值
for value in person.values():
    print(value)

# 遍历键值对
for key, value in person.items():
    print(f"{key}: {value}")

# 打印 0 到 4
for i in range(5):
    print(i)

# 打印 2 到 6（不包含 7）
for i in range(2, 7):
    print(i)

# 打印 1 到 10 中的奇数
for i in range(1, 11, 2):
    print(i)

for i in range(2):
    for j in range(3):
        print(f"({i}, {j})")

#while语句
count = 0
while count < 5:
    print(count)
    count += 1  # 必须更新条件，否则会无限循环

num = 3
while num > 0:
    print(num)
    num -= 1
print("Blast off!")

total = 0
number = 1
while number <= 10:
    total += number
    number += 1
print("1 到 10 的和是:", total)

password = ""
while password != "secret":
    password = input("请输入密码: ")
print("密码正确！")

for i in range(5):
    if i == 3:
        break  # 当 i 等于 3 时跳出整个循环
    print(i)

for i in range(5):
    if i == 3:
        continue  # 当 i 等于 3 时跳过本次循环，继续下一次
    print(i)

numbers = [2, 4, 6, 8]
target = 5

for num in numbers:
    if num == target:
        print(f"找到目标 {target}")
        break
else:
    print(f"未找到目标 {target}")


def get_grade():
    try:
        score = input("请输入成绩（0-100，输入q退出）：")
        
        # 条件分支：检查是否退出
        if score.lower() == 'q':
            print("程序已退出")
            return
        
        # 转换为数值（可能触发 ValueError）
        score = float(score)
        
        # 主动抛出异常：检查数值范围
        if score < 0 or score > 100:
            raise ValueError("成绩必须在 0-100 之间")
        
        # 多重条件分支：判断等级
        if score >= 90:
            grade = 'A'
        elif score >= 80:
            grade = 'B'
        elif score >= 70:
            grade = 'C'
        elif score >= 60:
            grade = 'D'
        else:
            grade = 'F'
            
    except ValueError as e:
        print(f"输入错误：{e}")
    except Exception as e:
        print(f"未知错误：{e}")
    else:
        print(f"成绩 {score} 对应的等级是：{grade}")
    finally:
        print("-" * 20)

# 测试
get_grade()